package com.huazai.out_school_manager.Servlet;

import com.huazai.out_school_manager.Util.DBUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


@WebServlet(name = "admin",value = "/admin")
public class AdminServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        Connection conn = null;
        ResultSet rs = null;
        PreparedStatement ps = null;

        String userid = (String) request.getAttribute("userid");
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter pw = response.getWriter();

        pw.print("<head>\n" +
                "    <meta charset=\"UTF-8\">\n" +
                "    <title>出校申请审批</title>\n" +
                "    <script type='text/javascript'>\n" +
                "      function del(applyID) {\n" +
                "        if(window.confirm('好哥们，确定要删除吗？（删除了之后可是不可恢复的哦）')) {\n" +
                "            document.location.href = 'delete?applyID=' + applyID;\n" +
                "        }\n" +
                "      }\n" +
                "    </script>\n" +
                "</head>\n");
        pw.print("<h1>" + userid + "  管理员，欢迎登录</h1>\n");
        pw.print("<h1>欢迎管理出校申请</h1>\n");



        try {
            conn = DBUtil.getConnectDB();
            String sql = "select * from submitinfo order by Hsubmit desc;";
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            int i = 0;
            pw.print("<table align='center' border='1' width='70%'>\n");
            pw.print("<tr>\n" +
                    "<th>出校申请序号</th>\n" +
                    "<th>提交用户ID</th>\n" +
                    "<th>提交时间</th>\n" +
                    "<th>申请时长（小时）</th>\n" +
                    "<th>审批状态（P,R,A）</th>\n" +
                    "<th>执行操作</th>\n" +
                    "</tr>\n");
            while( rs.next() ) {
                pw.print("<tr>\n");
                String JudgeUserid = rs.getString(1);
                String submitTime = rs.getString(2);
                long applyID = rs.getLong("applyID");

                pw.print("<td align='center' >" + (++i) + "</td>\n");
                pw.print("<td align='center' >" + JudgeUserid + "</td>\n");
                pw.print("<td align='center'>" + submitTime + "</td>\n");
                pw.print("<td align='center'>" + rs.getString(3) + "</td>\n");
                pw.print("<td align='center'>" + rs.getString(4) + "</td>\n");

                pw.print("<td align='center'>\n");
                pw.print("<a href='decide?applyID=" + applyID + "&result=Accepted" + "'>同 意</a>\n");
                pw.print("<a href='decide?applyID=" + applyID + "&result=Rejected" + "'>拒 绝</a>\n");

                pw.print("<a href=\"javascript:void(0)\" onclick='del(\""+ applyID + "\")'>删 除</a>\n");
                pw.print("</td>\n");
                pw.print("</tr>\n");
            }
            pw.print("</table>\n");

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn, ps, rs);
        }

    }

}
